//@author A0099983B
#include "TaskItGUI.h"
using namespace System;
using namespace System::Windows::Forms;

[STAThread]
int main(array<String^>^ args) {
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false);

	UI::TaskItGUI mainWindow;

	Application::Run(%mainWindow);
}

void UI::TaskItGUI::retrievePreviousUserEnteredInfo() {
			if (_currentLinePos > NO_LINE) {
				if (_currentLinePos + ACCOUNT_FOR_CONV_FROM_POS_TO_SIZE == getTotalLinesEntered()) {
						setTempUserLine(inputBox->Text);
				}
			}
			if (_currentLinePos > LINE_ZERO) { 
				inputBox->Clear();
				inputBox->Text = getUserInputLine(getCurrentLinePos()); 
				_currentLinePos--;
				inputBox->SelectionStart = inputBox->TextLength;
			}
}

void UI::TaskItGUI::retrieveNextUserEnteredInfo() {
			if (_currentLinePos + ACCOUNT_FOR_CONV_FROM_POS_TO_SIZE == getTotalLinesEntered() && (_currentLinePos > NO_LINE)) {
				inputBox->Clear();
				inputBox->Text = getTempUserLine();
				inputBox->SelectionStart = inputBox->TextLength;
			} else if (_currentLinePos + ACCOUNT_FOR_CONV_FROM_POS_TO_SIZE < getTotalLinesEntered() && _currentLinePos >  NO_LINE) {
				_currentLinePos++;
				inputBox->Clear();
				inputBox->Text = getUserInputLine(getCurrentLinePos());
				inputBox->SelectionStart = inputBox->TextLength;
			}
}
void UI::TaskItGUI::updateSmallWindow(String^ updatedInfo) {
				statusOutputBox->Text = updatedInfo;
}

void UI::TaskItGUI::processInputAndShowCorrectDisplayToUser() {
				bool isMainInfoDisplayAtSmallWindow = true;
				String^ outputString = EMPTY_STRING;
				string userInput;
				userInput = convertToString(inputBox->Text);
				Logger *_logFile;
				_logFile = Logger::getInstance();

				std::vector<string> requiredTaskInfo;
				try {
					_logFile->loggingEvents(convertToString(LOG_CALLED_SCHEDULER));
					requiredTaskInfo = userScheduler->executeCommand(userInput, isMainInfoDisplayAtSmallWindow);

					if (!isMainInfoDisplayAtSmallWindow) {
						_logFile->loggingEvents(convertToString(LOG_SMALL_UI_DISPLAY));
						listOutput->Items->Clear();
						displayTasks(requiredTaskInfo);
						updateSmallWindow(EMPTY_STRING);
					} else if (isMainInfoDisplayAtSmallWindow) {
						_logFile->loggingEvents(convertToString(LOG_BIG_UI_DISPLAY));
						assert(requiredTaskInfo.size() == ONE_REQUIRED_INFO);
						String^taskInfo;
						int infoNo = FIRST_POS;
						taskInfo = convertToCliString(requiredTaskInfo.at(infoNo));
						updateSmallWindow(taskInfo);

						listOutput->Items->Clear();
						requiredTaskInfo.clear();
						requiredTaskInfo = userScheduler->refreshBigWindow(isMainInfoDisplayAtSmallWindow); //to refresh the big UI.
						if (!isMainInfoDisplayAtSmallWindow) {
							displayTasks(requiredTaskInfo);
						}
					}
				} catch (string error) {
					throw error;
				}
}

void UI::TaskItGUI::showHelpToUser() {
				TaskItHelp^ helpWindow = gcnew TaskItHelp();
				helpWindow->Show();
}

void UI::TaskItGUI::highlightCompleteFields() {
				if ((inputBox->Text->Length >= LENGTH_OF_ADD &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_ADD) == COMMAND_ADD) ||
					(inputBox->Text->Length >= LENGTH_OF_BLOCK &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_BLOCK) == COMMAND_BLOCK)){
					if (checkNoandAltBrackets(SIX_SETS_OF_BRACKET)) {
						highlightFieldsCorrespondingToCommand(SEVEN_FIELDS);
					}
				} else if ((inputBox->Text->Length >= LENGTH_OF_EDIT && 
					inputBox->Text->Length <= LENGTH_OF_EDIT_BASIC_PROMPT_FOUR_DIGITS &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_EDIT) == COMMAND_EDIT) ||
					(inputBox->Text->Length >= LENGTH_OF_FEDIT && 
					inputBox->Text->Length <= LENGTH_OF_FEDIT_BASIC_PROMPT_FOUR_DIGITS &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_FEDIT) == COMMAND_FEDIT)){
					if (checkNoandAltBrackets(ONE_SET_OF_BRACKET)) {
						highlightFieldsCorrespondingToCommand(TWO_FIELDS); // for tabbing with only edit [no. ]
					}
				} else if ((inputBox->Text->Length >= LENGTH_OF_EDIT &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_EDIT) == COMMAND_EDIT) ||
					(inputBox->Text->Length >= LENGTH_OF_FEDIT &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_FEDIT) == COMMAND_FEDIT)) {
					if (checkNoandAltBrackets(SEVEN_SETS_OF_BRACKET)) { //for tabbing with all the fields present edit [][][][][][][][]
						highlightFieldsCorrespondingToCommand(EIGHT_FIELDS);
					}
				} else if ((inputBox->Text->Length >= LENGTH_OF_VIEW &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_VIEW) == COMMAND_VIEW) ||
					(inputBox->Text->Length >= LENGTH_OF_DELETE && 
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_DELETE) == COMMAND_DELETE) ||
					(inputBox->Text->Length >= LENGTH_OF_MARK &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_MARK) == COMMAND_MARK) ||
					(inputBox->Text->Length >= LENGTH_OF_UNMARK &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_UNMARK) == COMMAND_UNMARK) ||
					(inputBox->Text->Length >= LENGTH_OF_SEARCH && 
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_SEARCH) == COMMAND_SEARCH) ||
					(inputBox->Text->Length >= LENGTH_OF_CONFIRM &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_CONFIRM) == COMMAND_CONFIRM) ||
					(inputBox->Text->Length >= LENGTH_OF_FDELETE &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_FDELETE) == COMMAND_FDELETE)) {
					if (checkNoandAltBrackets(ONE_SET_OF_BRACKET)) {
						highlightFieldsCorrespondingToCommand(TWO_FIELDS);
					}
				}
}

bool UI::TaskItGUI::checkNoandAltBrackets(int correctNoSetOfBrackets) {
				int noOfOpenBrackets = NO_OPEN_BRACKET, noOfCloseBrackets = NO_CLOSE_BRACKET;
				int noOfBracketFound = NO_BRACKET_FOUND;
				bool result = true;
				string input = convertToString(inputBox->Text);
				int inputLength = input.size();

				for (int i = INITIAL_VALUE; i < inputLength; i++) {
					if (input[i] == CHAR_OPEN_SQ_BRACKET) {
						noOfOpenBrackets++;
						noOfBracketFound++;
					} else if (input[i] == CHAR_CLOSE_SQ_BRACKET) {
						noOfCloseBrackets++;
						noOfBracketFound++;
					}
					if (noOfBracketFound % EVEN_NO_TWO == NO_REMAINDER && !(noOfOpenBrackets == noOfCloseBrackets)) {
						return false;  // open and close brackets neverv alternate
					}
					if (noOfBracketFound % EVEN_NO_TWO == ONE_REMAINDER && !(noOfOpenBrackets == (noOfCloseBrackets + ONE_DIFFERENCE_BTN_OPEN_CLOSE_BRACKETS))) {
						return false; //open and close brackets never alternate
					}
				}
				if ((noOfCloseBrackets != correctNoSetOfBrackets) ||
					(noOfOpenBrackets != noOfCloseBrackets ||
					(noOfOpenBrackets != correctNoSetOfBrackets))) {
					return false;
				}
				return result;
}

void UI::TaskItGUI::highlightFieldsCorrespondingToCommand(int commandNoOfFields) {
				int firstOpenBracketPos;
				int start = INITIAL_VALUE, end, highlightLength, highlightStart;
				end = inputBox->Text->IndexOf(SPACE); 
				firstOpenBracketPos = inputBox->Text->IndexOf(OPEN_SQ_BRACKET);

				if (end >= MIN_LENGTH_OF_COMMAND && end < firstOpenBracketPos) {
					_currFieldNo++;
					for (int i = INITIAL_VALUE; i < _currFieldNo % (commandNoOfFields); i++) {
						//search for the next open sq bracket from the previous end, availability assured by checkNoAndAltBracket()
						start = inputBox->Text->IndexOf(OPEN_SQ_BRACKET, end) + SKIP_OPEN_SQ_BRACKET; 
						//search for the next close sq bracket from the previous start, availability assured by checkNoAndAltBracket()
						end = inputBox->Text->IndexOf(CLOSE_SQ_BRACKET, start) + SKIP_CLOSE_SQ_BRACKET;
						int theLastFieldToHighlight = (_currFieldNo % commandNoOfFields) - SKIP_LAST_FIELD;
						if (i != theLastFieldToHighlight) { // proceed on to the next field if an empty field is met.
							end++;
						}
					}
					string input = convertToString(inputBox->Text);
					if (input[end] == CHAR_SPACE) {
						end--; // to not highlight space after command
					}
					highlightLength = end - start + ACCOUNT_FOR_CONV_FROM_POS_TO_LENGTH;
					highlightStart = start;
					inputBox->SelectionStart = highlightStart;
					inputBox->SelectionLength = highlightLength;
				}
}

void UI::TaskItGUI::setBoldedDatesForCalendar() {
				std::vector <int> allDates = userScheduler->getDatesWithTasks();
				int i = INITIAL_VALUE;
				int day, month, year;
				int totalNoOfDates = allDates.size();
				int currentDate;

				DateTime datesBolded;
				while (i < totalNoOfDates) {
					currentDate = allDates[i];
					year = currentDate % MOD_ONE_HUNDRED + YEAR_TWO_THOUSAND;
					currentDate = currentDate / MOD_ONE_HUNDRED;
					month = currentDate % MOD_ONE_HUNDRED;
					currentDate = currentDate / MOD_ONE_HUNDRED;
					day = currentDate;
					datesBolded = DateTime(year, month, day);

					monthlyCalendar->AddBoldedDate(datesBolded);
					i++;
				}
}

void UI::TaskItGUI::refreshCalendarBoldedDates() {
				monthlyCalendar->RemoveAllBoldedDates();
				setBoldedDatesForCalendar();
				monthlyCalendar->UpdateBoldedDates();
}

void UI::TaskItGUI::refreshFloatingTasks() {
				floatingTaskOutputBox->Text = convertToCliString(userScheduler->refreshFloatingTasksWindow());
}

void UI::TaskItGUI::resetEditItemNumber() {
				_currentEditItemNo = INVALID_EDIT_ITEM;
				_previousEditItemNo = INVALID_EDIT_ITEM;
}

void UI::TaskItGUI::setCurrFieldNo() {
				_currFieldNo = NO_FIELD;
}

void UI::TaskItGUI::displayTasks(std::vector<string> allTasks) {
				assert((allTasks.size() + SHIFT_INDEX_BY_ONE) % NUM_OF_COLUMNS != NO_REMAINDER);
				int infoNo = INVALID_NO_NEG_ONE;
				String^ taskInfo;
				bool isActivityMultilineNeeded = false;
				bool isVenueMultilineNeeded = false;
				bool isCategoryMultilineNeeded = false;
				String^ remainingLongActivity;
				String^ remainingLongVenue;
				String^ remainingLongCategory;

				typeOfTask currentTypeOfTask;
				int maxPossibleActivityLength = (Activity->Width) / WIDTH_TO_CHARACTER_DISPLAY_RATIO;
				int maxPossibleVenueLength = (Venue->Width) / WIDTH_TO_CHARACTER_DISPLAY_RATIO;
				int maxPossibleCategoryLength = (Category->Width) / WIDTH_TO_CHARACTER_DISPLAY_RATIO;

				int taskNo = INVALID_NO_NEG_ONE;
				std::vector <int> datesOfAllTasksDisplayed;
				std::vector <string> markedStatusOfAllTasksDisplayed;
				int sizeOfAllTasks = allTasks.size();
				int date;
				string taskMarkedStatus;
				if (sizeOfAllTasks > NO_DATE) {
					for (int i = INITIAL_VALUE; i < sizeOfAllTasks / MAX_TOTAL_NO_OF_FIELDS; i++) {
						date = atoi(allTasks.at(POS_OF_FIRST_DATE_IN_VECTOR + i*MAX_TOTAL_NO_OF_FIELDS).c_str());
						datesOfAllTasksDisplayed.push_back(date);
						taskMarkedStatus = allTasks.at(POS_OF_FIRST_MARKED_IN_VECTOR + i*MAX_TOTAL_NO_OF_FIELDS);
						markedStatusOfAllTasksDisplayed.push_back(taskMarkedStatus);

					}
				}
				while ((infoNo + ACCOUNT_FOR_CONV_FROM_POS_TO_SIZE) != allTasks.size()) {
					taskInfo = convertToCliString(allTasks.at(++infoNo));
					listViewItem = gcnew Windows::Forms::ListViewItem(taskInfo);// insert index
					++taskNo;
					currentTypeOfTask = checkTypeOfTasks(datesOfAllTasksDisplayed.at(taskNo), markedStatusOfAllTasksDisplayed.at(taskNo));
					updateBackgroundColourToTypeOfTask(currentTypeOfTask);

					//output activity information
					taskInfo = convertToCliString(allTasks.at(++infoNo));
					isActivityMultilineNeeded = checkMultilineNeededForActivity(taskInfo);
					if (isActivityMultilineNeeded) {
						getFirstLineWithoutTruncation(taskInfo, maxPossibleActivityLength, remainingLongActivity);
					} else {
						remainingLongActivity = EMPTY_STRING;
						listViewItem->SubItems->Add(taskInfo);					// Single Line Activity
					}

					//output venue infomation
					taskInfo = convertToCliString(allTasks.at(++infoNo));
					isVenueMultilineNeeded = checkMultilineNeededForVenue(taskInfo);
					if (isVenueMultilineNeeded) {
						getFirstLineWithoutTruncation(taskInfo, maxPossibleVenueLength, remainingLongVenue);
					} else {
						remainingLongVenue = EMPTY_STRING;
						listViewItem->SubItems->Add(taskInfo);					// Single Line Venue
					}

					//output start time infomation
					taskInfo = convertToCliString(allTasks.at(++infoNo));
					listViewItem->SubItems->Add(taskInfo);

					//output end time infomation
					taskInfo = convertToCliString(allTasks.at(++infoNo));
					listViewItem->SubItems->Add(taskInfo);

					//output cateogry infomation
					taskInfo = convertToCliString(allTasks.at(++infoNo));
					isCategoryMultilineNeeded = checkMultilineNeededForCategory(taskInfo);
					if (isCategoryMultilineNeeded) {
						getFirstLineWithoutTruncation(taskInfo, maxPossibleCategoryLength, remainingLongCategory);
					} else {
						remainingLongCategory = EMPTY_STRING;
						listViewItem->SubItems->Add(taskInfo);					// Single Line Category
					}

					//output date infomation
					taskInfo = convertToCliString(allTasks.at(++infoNo));
					listViewItem->SubItems->Add(taskInfo);						//date
					taskInfo = convertToCliString(allTasks.at(++infoNo));

					//output marked infomation
					listViewItem->SubItems->Add(taskInfo);						//marked
					listOutput->Items->Add(this->listViewItem);

					if (isActivityMultilineNeeded || isVenueMultilineNeeded || isCategoryMultilineNeeded) { 
						//add remaining of the activity/venue/category in the next line
						displayRemainingMultilineForThisTask(remainingLongActivity, remainingLongVenue, remainingLongCategory, currentTypeOfTask);		
					}
				}
}

bool UI::TaskItGUI::checkLastCharFirstLineMaxPossibleLengthIsSpace(String^ taskInfo, int maxPossibleTextLength) {
				if (String::Equals(taskInfo->Substring(maxPossibleTextLength - ACCOUNT_FOR_CONV_FROM_LENGTH_TO_POS, LENGTH_OF_A_SPACE), (SPACE))) {
					return true;
				}
				return false;
}

bool UI::TaskItGUI::checkFirstCharInSecLineOfMaxPossibleLengthIsSpace(String^ taskInfo, int maxPossibleTextLength) {
				if (((taskInfo->Length - ACCOUNT_FOR_CONV_FROM_LENGTH_TO_POS) > maxPossibleTextLength + LENGTH_OF_FIRST_CHAR_IN_NEXT_LINE) &&
					String::Equals(taskInfo->Substring(maxPossibleTextLength, LENGTH_OF_A_SPACE), (SPACE))) {
					return true;
				}
				return false;
}

bool UI::TaskItGUI::checkForSpaceinFirstLine(String^ taskInfo, int maxPossibleTextLength) {
				if ((taskInfo->Substring(BEGINNING_POS_OF_STR, maxPossibleTextLength))->Contains(SPACE)) {
					return true;
				}
				return false;
}

void UI::TaskItGUI::getFirstLineWithoutTruncation(String^ taskInfo, int maxPossibleTextLength, String^ & remainingLongText) {
				// for insertion of words that wont be truncated
				if ((checkLastCharFirstLineMaxPossibleLengthIsSpace(taskInfo, maxPossibleTextLength)) ||
					checkFirstCharInSecLineOfMaxPossibleLengthIsSpace(taskInfo, maxPossibleTextLength) ||
					!(checkForSpaceinFirstLine(taskInfo, maxPossibleTextLength))) {
					listViewItem->SubItems->Add(taskInfo->Substring(BEGINNING_POS_OF_STR, maxPossibleTextLength));
					int remainingStartingPos = maxPossibleTextLength;
					remainingLongText = taskInfo->Substring(remainingStartingPos);
				} else {//remove the word that gets truncated in this line
					int lastSpaceInThisLinePos = INVALID_NO_SPACE; // to get the pos of the space
					for (int j = maxPossibleTextLength; j >= INITIAL_VALUE; j--) {
						if (String::Equals(taskInfo->Substring(j, LENGTH_OF_A_CHAR), (SPACE))) {
							lastSpaceInThisLinePos = j;
							break;
						}
					}

					if (lastSpaceInThisLinePos > SPACE_AT_ZERO_POS) {
						listViewItem->SubItems->Add(taskInfo->Substring(BEGINNING_POS_OF_STR, lastSpaceInThisLinePos));
						int  remainingStartingPos = lastSpaceInThisLinePos;
						remainingLongText = taskInfo->Substring(lastSpaceInThisLinePos);

					} else if (lastSpaceInThisLinePos == SPACE_AT_ZERO_POS) {
						listViewItem->SubItems->Add(taskInfo->Substring(START_AFTER_SPACE, maxPossibleTextLength));
						int remainingStartingPos = maxPossibleTextLength + ACCOUNT_FOR_CONV_FROM_LENGTH_TO_POS;
						remainingLongText = taskInfo->Substring(remainingStartingPos);//skip the extra space infront
					}
				}
}

void UI::TaskItGUI::displayRemainingMultilineForThisTask(String^ remainingLongActivity, String^ remainingLongVenue, String^ remainingLongCategory, typeOfTask currentTypeOfTask) {
				int maxPossibleActivityLength = (Activity->Width) / WIDTH_TO_CHARACTER_DISPLAY_RATIO;
				int maxPossibleVenueLength = (Venue->Width) / WIDTH_TO_CHARACTER_DISPLAY_RATIO;
				int maxPossibleCategoryLength = (Category->Width) / WIDTH_TO_CHARACTER_DISPLAY_RATIO;

				int noOfExtraLinesNeededActivity = (remainingLongActivity->Length / maxPossibleActivityLength);
				int noOfExtraLinesNeededVenue = (remainingLongVenue->Length / maxPossibleVenueLength);
				int noOfExtraLinesNeededCategory = (remainingLongCategory->Length / maxPossibleCategoryLength);

				int activityStartPos = BEGINNING_POS;
				int venueStartPos = BEGINNING_POS;
				int categoryStartPos = BEGINNING_POS;

				int activityCounter;
				int venueCounter;
				int categoryCounter;

				int extraLinesForUnaccountedLastRow = NO_UNACCOUNTED_LAST_ROW;
				String^ activityInThisLine;
				String^ venueInThisLine;
				String^ categoryInThisLine;

				if (remainingLongVenue->Length % (maxPossibleVenueLength) != NO_REMAINDER) {
					noOfExtraLinesNeededVenue++;
				}
				if ((remainingLongActivity->Length % (maxPossibleActivityLength)) != NO_REMAINDER) {
					extraLinesForUnaccountedLastRow = ONE_UNACCOUNTED_LAST_ROW;
					noOfExtraLinesNeededActivity = extraLinesForUnaccountedLastRow + noOfExtraLinesNeededActivity;
				}
				if (remainingLongCategory->Length % (maxPossibleCategoryLength) != NO_REMAINDER) {
					noOfExtraLinesNeededCategory++;
				}

				int noOfOverallExtraLinesNeeded = max(noOfExtraLinesNeededVenue, noOfExtraLinesNeededCategory);
				noOfOverallExtraLinesNeeded = max(noOfOverallExtraLinesNeeded, noOfExtraLinesNeededActivity);
				
				//noOfExtraLinesNeeded Will be at least 1.
				for (int i = ONE_OVERALL_EXTRA_LINE_NEEDED; i <= noOfOverallExtraLinesNeeded; i++) {
					activityCounter = i;
					venueCounter = i;
					categoryCounter = i;

					activityInThisLine = getCurrentLineWithoutTruncation(remainingLongActivity, activityStartPos, maxPossibleActivityLength, 
																		 noOfOverallExtraLinesNeeded, activityCounter, noOfExtraLinesNeededActivity);
					venueInThisLine = getCurrentLineWithoutTruncation(remainingLongVenue, venueStartPos, maxPossibleVenueLength, 
																	  noOfOverallExtraLinesNeeded, venueCounter, noOfExtraLinesNeededVenue);
					categoryInThisLine = getCurrentLineWithoutTruncation(remainingLongCategory, categoryStartPos, maxPossibleCategoryLength, 
																		 noOfOverallExtraLinesNeeded, categoryCounter, noOfExtraLinesNeededCategory);

					noOfOverallExtraLinesNeeded = max(noOfExtraLinesNeededVenue, noOfExtraLinesNeededCategory);
					noOfOverallExtraLinesNeeded = max(noOfOverallExtraLinesNeeded, noOfExtraLinesNeededActivity);

					if (activityCounter == COUNTER_RESET || venueCounter == COUNTER_RESET || categoryCounter == COUNTER_RESET) {
						i = COUNTER_RESET;
					}

					listViewItem = gcnew Windows::Forms::ListViewItem(EMPTY_STRING);

					updateBackgroundColourToTypeOfTask(currentTypeOfTask);

					listViewItem->SubItems->Add(activityInThisLine);
					listViewItem->SubItems->Add(venueInThisLine);
					listViewItem->SubItems->Add(EMPTY_STRING);
					listViewItem->SubItems->Add(EMPTY_STRING);
					listViewItem->SubItems->Add(categoryInThisLine);
					listViewItem->SubItems->Add(EMPTY_STRING);
					listViewItem->SubItems->Add(EMPTY_STRING);
					listOutput->Items->Add(this->listViewItem);
				}
}

String^ UI::TaskItGUI::getCurrentLineWithoutTruncation(String^ remainingLongText, 
													   int& textStartPos, 
													   int maxPossibleTextLength, 
													   int noOfOverallExtraLinesNeeded, 
													   int &i, 
													   int &noOfExtraLinesNeededText) {
				String^ textInThisLine;
				int extraLinesForUnaccountedLastRow = NO_UNACCOUNTED_LAST_ROW;

				if ((remainingLongText->Length - textStartPos ) > maxPossibleTextLength) {
					// for insertion of words that wont be truncated
					if (String::Equals(remainingLongText->Substring(textStartPos + maxPossibleTextLength - ACCOUNT_FOR_CONV_FROM_LENGTH_TO_POS, LENGTH_OF_A_SPACE), (SPACE)) ||
						(((remainingLongText->Length - textStartPos - ACCOUNT_FOR_CONV_FROM_LENGTH_TO_POS) > maxPossibleTextLength + LENGTH_OF_FIRST_CHAR_IN_NEXT_LINE) &&
						String::Equals(remainingLongText->Substring(textStartPos + maxPossibleTextLength, LENGTH_OF_A_SPACE), (SPACE))) ||
						!((remainingLongText->Substring(textStartPos, maxPossibleTextLength))->Contains(SPACE))) {
						textInThisLine = remainingLongText->Substring(textStartPos, maxPossibleTextLength);
						textStartPos = textStartPos + maxPossibleTextLength;
					} else {//remove the word that gets truncated in this line
						int lastSpaceInThisLinePos = INVALID_NO_SPACE; // to get the pos of the space
						for (int j = maxPossibleTextLength; j >= INITIAL_VALUE; j--) {
							if (String::Equals(remainingLongText->Substring(textStartPos + j, LENGTH_OF_A_SPACE), (SPACE))) {
								lastSpaceInThisLinePos = j;
								break;
							}
						}

						if (lastSpaceInThisLinePos > SPACE_AT_ZERO_POS) {
							textInThisLine = remainingLongText->Substring(textStartPos, lastSpaceInThisLinePos);
							textStartPos = textStartPos + lastSpaceInThisLinePos;
							if (((remainingLongText->Substring(textStartPos)->Length) % (maxPossibleTextLength)) != NO_REMAINDER){
								extraLinesForUnaccountedLastRow = ONE_UNACCOUNTED_LAST_ROW;
							} else {
								extraLinesForUnaccountedLastRow = NO_UNACCOUNTED_LAST_ROW;
							}
							noOfExtraLinesNeededText = ((((remainingLongText->Substring(textStartPos)->Length)) / maxPossibleTextLength) + extraLinesForUnaccountedLastRow);
							i = INITIAL_VALUE;
						} else if (lastSpaceInThisLinePos == SPACE_AT_ZERO_POS) {
							textStartPos = textStartPos++;
							textInThisLine = remainingLongText->Substring(textStartPos, maxPossibleTextLength);
							textStartPos = textStartPos + maxPossibleTextLength;
							if (((remainingLongText->Substring(textStartPos)->Length) % (maxPossibleTextLength)) != 0) {
								extraLinesForUnaccountedLastRow = ONE_UNACCOUNTED_LAST_ROW;
							} else {
								extraLinesForUnaccountedLastRow = NO_UNACCOUNTED_LAST_ROW;
							}
							noOfExtraLinesNeededText = ((((remainingLongText->Substring(textStartPos)->Length)) / maxPossibleTextLength) + extraLinesForUnaccountedLastRow);
							i = INITIAL_VALUE;
						}
					}
				} else if (remainingLongText->Length - textStartPos > textStartPos) {
					textInThisLine = remainingLongText->Substring(textStartPos);
					textStartPos = textStartPos + maxPossibleTextLength;
					noOfExtraLinesNeededText--;
				} else if (remainingLongText->Length < textStartPos) { // nothing more to add
					textInThisLine = EMPTY_STRING;
					noOfExtraLinesNeededText--;
				} else if (remainingLongText->Length - textStartPos < textStartPos) {
					textInThisLine = remainingLongText->Substring(textStartPos); // display only 1 line
					textStartPos = textStartPos + maxPossibleTextLength;
					noOfExtraLinesNeededText = NO_UNACCOUNTED_LAST_ROW;
				}
				return  textInThisLine;
}

bool UI::TaskItGUI::checkMultilineNeededForActivity(String^ activity) {
				if (activity->Length >(Activity->Width / WIDTH_TO_CHARACTER_DISPLAY_RATIO)) {
					return true;
				}
				return false;
}

bool UI::TaskItGUI::checkMultilineNeededForVenue(String^ venue) {
				if (venue->Length > (Venue->Width / WIDTH_TO_CHARACTER_DISPLAY_RATIO)) {
					return true;
				}
				return false;
}

bool UI::TaskItGUI::checkMultilineNeededForCategory(String^ category) {
				if (category->Length > (Category->Width / WIDTH_TO_CHARACTER_DISPLAY_RATIO)) {
					return true;
				}
				return false;
}

void UI::TaskItGUI::updateBackgroundColourToTypeOfTask(typeOfTask currentTypeOfTask) {
				if (currentTypeOfTask == typeOfTask::presentAndNotDone) {
					this->listViewItem->BackColor = System::Drawing::Color::Yellow;
				} else if (currentTypeOfTask == typeOfTask::pastAndNotDone) {
					this->listViewItem->BackColor = System::Drawing::Color::OrangeRed;
				} else if (currentTypeOfTask == typeOfTask::pastAndDone ||
					currentTypeOfTask == typeOfTask::presentAndDone ||
					currentTypeOfTask == typeOfTask::futureAndDone) {
					this->listViewItem->BackColor = System::Drawing::Color::GreenYellow;
				} else {
					this->listViewItem->BackColor = System::Drawing::Color::White;
				}
}

bool UI::TaskItGUI::checkFirstWordIsSearch() {
				if (inputBox->Text->Length >= LENGTH_OF_SEARCH && 
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_SEARCH) == COMMAND_SEARCH){
					return true;
				}
				return false;
}

void UI::TaskItGUI::provideLiveSearch() {
				bool isMainInfoDisplayAtSmallWindow = true;
				String^ outputString = EMPTY_STRING;
				string userInput;
				userInput = convertToString(inputBox->Text);

				std::vector<string> searchedTaskInfo;
				try {
					searchedTaskInfo = userScheduler->executeCommand(userInput, isMainInfoDisplayAtSmallWindow);

					if (!isMainInfoDisplayAtSmallWindow) {
						listOutput->Items->Clear();
						displayTasks(searchedTaskInfo);
						updateSmallWindow(EMPTY_STRING);
					} else if (isMainInfoDisplayAtSmallWindow) {
						assert(searchedTaskInfo.size() == ONE_REQUIRED_INFO);
						String^ taskInfo;
						int infoNo = FIRST_POS;
						taskInfo = convertToCliString(searchedTaskInfo.at(infoNo));
						updateSmallWindow(taskInfo);

						listOutput->Items->Clear();
						searchedTaskInfo.clear();
					}
				} catch (string error) {
					throw error;
				}
}
	
void UI::TaskItGUI::provideBasicPromptsToUser() {
				if (inputBox->Text->Length == LENGTH_OF_ADD && 
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_ADD) == COMMAND_ADD) {
					helpUserWithAdd();
				} else if (inputBox->Text->Length == LENGTH_OF_EDIT  &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_EDIT) == COMMAND_EDIT) {
					helpUserWithEdit();
				} else if (inputBox->Text->Length == LENGTH_OF_FDELETE &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_FDELETE) == COMMAND_FDELETE) {
					helpUserWithFDelete();
				} else if (inputBox->Text->Length == LENGTH_OF_FEDIT &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_FEDIT) == COMMAND_FEDIT) {
					helpUserWithFEdit();
				} else if (inputBox->Text->Length == LENGTH_OF_VIEW  &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_VIEW) == COMMAND_VIEW) {
					helpUserWithView();
				} else if (inputBox->Text->Length == LENGTH_OF_DELETE &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_DELETE) == COMMAND_DELETE) {
					helpUserWithDelete();
				} else if (inputBox->Text->Length == LENGTH_OF_SEARCH &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_SEARCH) == COMMAND_SEARCH) {
					helpUserWithSearch();
				} else if (inputBox->Text->Length == LENGTH_OF_MARK &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_MARK) == COMMAND_MARK) {
					helpUserWithMark();
				} else if (inputBox->Text->Length == LENGTH_OF_BLOCK &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_BLOCK) == COMMAND_BLOCK) {
					helpUserWithBlock();
				} else if (inputBox->Text->Length == LENGTH_OF_UNMARK &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_UNMARK) == COMMAND_UNMARK) {
					helpUserWithUnmark();
				} else if (inputBox->Text->Length == LENGTH_OF_CONFIRM &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_CONFIRM) == COMMAND_CONFIRM) {
					helpUserWithConfirm();
				}
}

bool UI::TaskItGUI::checkIfNoCommandBeforeThisCommand() {
				if (_prevCommand == EMPTY_STRING) {
					return true;
				}
				return false;
}

void UI::TaskItGUI::checkEditNoValidAndDisplayCorrectEditPrompts() {
				if (inputBox->Text->Length >= (LENGTH_OF_EDIT_BASIC_PROMPT_ONE_DIGIT) &&
					inputBox->Text->Substring(POS_FIRST_OPEN_BRACKET_FOR_EDIT, LENGTH_OF_OPEN_SQ_BRACKET) == OPEN_SQ_BRACKET &&
					inputBox->Text->Substring(POS_FIRST_CLOSE_BRACKET_EDIT_ONE_DIGIT, LENGTH_OF_CLOSE_SQ_BRACKET) == CLOSE_SQ_BRACKET) { // for single digit item number eg. edit [8]
					bool isNumber = checkIfEditInputsAreNumbers(ONE_DIGIT);
					if (isNumber) {
						getAdvancedEditPrompts(ONE_DIGIT);
					} else { // only retain the basic prompts with current no
						inputBox->Text = inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_EDIT_BASIC_PROMPT_ONE_DIGIT);
						inputBox->SelectionStart = POS_LAST_CHAR_BEFORE_FIRST_CLOSE_BRACKET_EDIT_ONE_DIGIT;
						resetEditItemNumber();
					}
				} else if (inputBox->Text->Length >= LENGTH_OF_EDIT_BASIC_PROMPT_TWO_DIGITS &&
					inputBox->Text->Substring(POS_FIRST_OPEN_BRACKET_FOR_EDIT, LENGTH_OF_OPEN_SQ_BRACKET) == OPEN_SQ_BRACKET &&
					inputBox->Text->Substring(POS_FIRST_CLOSE_BRACKET_EDIT_TWO_DIGITS, LENGTH_OF_CLOSE_SQ_BRACKET) == CLOSE_SQ_BRACKET) { // for double digit item number eg. edit [80]
					bool isNumber = checkIfEditInputsAreNumbers(TWO_DIGITS);
					if (isNumber) {
						getAdvancedEditPrompts(TWO_DIGITS);
					} else {// only retain the basic prompts with current no
						inputBox->Text = inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_EDIT_BASIC_PROMPT_TWO_DIGITS);
						inputBox->SelectionStart = POS_LAST_CHAR_BEFORE_FIRST_CLOSE_BRACKET_EDIT_TWO_DIGITS;
						resetEditItemNumber();
					}
				} else if (inputBox->Text->Length >= LENGTH_OF_EDIT_BASIC_PROMPT_THREE_DIGITS &&
					inputBox->Text->Substring(POS_FIRST_OPEN_BRACKET_FOR_EDIT, LENGTH_OF_OPEN_SQ_BRACKET) == OPEN_SQ_BRACKET &&
					inputBox->Text->Substring(POS_FIRST_CLOSE_BRACKET_EDIT_THREE_DIGITS, LENGTH_OF_CLOSE_SQ_BRACKET) == CLOSE_SQ_BRACKET) { // for triple digit item number eg. edit [800]
					bool isNumber = checkIfEditInputsAreNumbers(THREE_DIGITS);
					if (isNumber) {
						getAdvancedEditPrompts(THREE_DIGITS);
					} else {// only retain the basic prompts with current no
						inputBox->Text = inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_EDIT_BASIC_PROMPT_THREE_DIGITS);
						inputBox->SelectionStart = POS_LAST_CHAR_BEFORE_FIRST_CLOSE_BRACKET_EDIT_THREE_DIGITS;
						resetEditItemNumber();
					}
				} else if (inputBox->Text->Length >= LENGTH_OF_EDIT_BASIC_PROMPT_FOUR_DIGITS &&
					inputBox->Text->Substring(POS_FIRST_OPEN_BRACKET_FOR_EDIT, LENGTH_OF_OPEN_SQ_BRACKET) == OPEN_SQ_BRACKET &&
					inputBox->Text->Substring(POS_FIRST_CLOSE_BRACKET_EDIT_FOUR_DIGITS, LENGTH_OF_CLOSE_SQ_BRACKET) == CLOSE_SQ_BRACKET) { // for four digit item number eg. edit [8000]
					bool isNumber = checkIfEditInputsAreNumbers(FOUR_DIGITS);
					if (isNumber) {
						getAdvancedEditPrompts(FOUR_DIGITS);
					} else {// only retain the basic prompts with current no
						inputBox->Text = inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_EDIT_BASIC_PROMPT_FOUR_DIGITS);
						inputBox->SelectionStart = POS_LAST_CHAR_BEFORE_FIRST_CLOSE_BRACKET_EDIT_FOUR_DIGITS;
						resetEditItemNumber();
					}
				}
}

void UI::TaskItGUI::checkFEditNoValidAndDisplayCorrectFEditPrompts() {
				if (inputBox->Text->Length >= LENGTH_OF_FEDIT_BASIC_PROMPT_ONE_DIGIT &&
					inputBox->Text->Substring(POS_FIRST_OPEN_BRACKET_FOR_FEDIT, LENGTH_OF_OPEN_SQ_BRACKET) == OPEN_SQ_BRACKET &&
					inputBox->Text->Substring(POS_FIRST_CLOSE_BRACKET_FEDIT_ONE_DIGIT, LENGTH_OF_CLOSE_SQ_BRACKET) == CLOSE_SQ_BRACKET) { // for single digit item number eg. fedit [8]
					bool isNumber = checkIfFEditInputsAreNumbers(ONE_DIGIT);
					if (isNumber) {
						getAdvancedFEditPrompts(ONE_DIGIT);
					} else {// only retain the basic prompts with current no
						inputBox->Text = inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_FEDIT_BASIC_PROMPT_ONE_DIGIT);
						inputBox->SelectionStart = POS_LAST_CHAR_BEFORE_FIRST_CLOSE_BRACKET_FEDIT_ONE_DIGIT;
						resetEditItemNumber();
					}
				} else if (inputBox->Text->Length >= LENGTH_OF_FEDIT_BASIC_PROMPT_TWO_DIGITS &&
					inputBox->Text->Substring(POS_FIRST_OPEN_BRACKET_FOR_FEDIT, LENGTH_OF_OPEN_SQ_BRACKET) == OPEN_SQ_BRACKET &&
					inputBox->Text->Substring(POS_FIRST_CLOSE_BRACKET_FEDIT_TWO_DIGITS, LENGTH_OF_CLOSE_SQ_BRACKET) == CLOSE_SQ_BRACKET) { // for double digit item number eg. fedit [80]
					bool isNumber = checkIfFEditInputsAreNumbers(TWO_DIGITS);
					if (isNumber) {
						getAdvancedFEditPrompts(TWO_DIGITS);
					} else {// only retain the basic prompts with current no
						inputBox->Text = inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_FEDIT_BASIC_PROMPT_TWO_DIGITS);
						inputBox->SelectionStart = POS_LAST_CHAR_BEFORE_FIRST_CLOSE_BRACKET_FEDIT_TWO_DIGITS;
						resetEditItemNumber();
					}
				} else if (inputBox->Text->Length >= LENGTH_OF_FEDIT_BASIC_PROMPT_THREE_DIGITS &&
					inputBox->Text->Substring(POS_FIRST_OPEN_BRACKET_FOR_FEDIT, LENGTH_OF_OPEN_SQ_BRACKET) == OPEN_SQ_BRACKET &&
					inputBox->Text->Substring(POS_FIRST_CLOSE_BRACKET_FEDIT_THREE_DIGITS, LENGTH_OF_CLOSE_SQ_BRACKET) == CLOSE_SQ_BRACKET) { // for triple digit item number eg. fedit [800]
					bool isNumber = checkIfFEditInputsAreNumbers(THREE_DIGITS);
					if (isNumber) {
						getAdvancedFEditPrompts(THREE_DIGITS);
					} else {// only retain the basic prompts with current no
						inputBox->Text = inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_FEDIT_BASIC_PROMPT_THREE_DIGITS);
						inputBox->SelectionStart = POS_LAST_CHAR_BEFORE_FIRST_CLOSE_BRACKET_FEDIT_THREE_DIGITS;
						resetEditItemNumber();
					}
				} else if (inputBox->Text->Length >= LENGTH_OF_FEDIT_BASIC_PROMPT_FOUR_DIGITS &&
					inputBox->Text->Substring(POS_FIRST_OPEN_BRACKET_FOR_FEDIT, LENGTH_OF_OPEN_SQ_BRACKET) == OPEN_SQ_BRACKET && 
					inputBox->Text->Substring(POS_FIRST_CLOSE_BRACKET_FEDIT_FOUR_DIGITS, LENGTH_OF_CLOSE_SQ_BRACKET) == CLOSE_SQ_BRACKET) { // for four digit item number eg. edit [8000]
					bool isNumber = checkIfFEditInputsAreNumbers(FOUR_DIGITS);
					if (isNumber) {
						getAdvancedFEditPrompts(FOUR_DIGITS);
					} else {// only retain the basic prompts with current no
						inputBox->Text = inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_FEDIT_BASIC_PROMPT_FOUR_DIGITS);
						inputBox->SelectionStart = POS_LAST_CHAR_BEFORE_FIRST_CLOSE_BRACKET_FEDIT_FOUR_DIGITS;
						resetEditItemNumber();
					}
				}
}

void UI::TaskItGUI::changePromptsAccordingToNewCommand() {
				if (inputBox->Text->Length >= LENGTH_OF_ADD &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_ADD) == COMMAND_ADD) {
					helpUserWithAdd();
				} else if (inputBox->Text->Length >= LENGTH_OF_EDIT && 
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_EDIT) == COMMAND_EDIT) {
					helpUserWithEdit();
				} else if (inputBox->Text->Length >= LENGTH_OF_FDELETE &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_FDELETE) == COMMAND_FDELETE) {
					helpUserWithFDelete();
				} else if (inputBox->Text->Length >= LENGTH_OF_FEDIT &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_FEDIT) == COMMAND_FEDIT) {
					helpUserWithFEdit();
				} else if (inputBox->Text->Length >= LENGTH_OF_VIEW &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_VIEW) == COMMAND_VIEW) {
					helpUserWithView();
				} else if (inputBox->Text->Length >= LENGTH_OF_DELETE && 
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_DELETE) == COMMAND_DELETE) {
					helpUserWithDelete();
				} else if (inputBox->Text->Length >= LENGTH_OF_SEARCH &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_SEARCH) == COMMAND_SEARCH) {
					helpUserWithSearch();
				} else if (inputBox->Text->Length >= LENGTH_OF_MARK &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_MARK) == COMMAND_MARK) {
					helpUserWithMark();
				} else if (inputBox->Text->Length >= LENGTH_OF_BLOCK &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_BLOCK) == COMMAND_BLOCK) {
					helpUserWithBlock();
				} else if (inputBox->Text->Length >= LENGTH_OF_UNMARK &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_UNMARK) == COMMAND_UNMARK) {
					helpUserWithUnmark();
				} else if (inputBox->Text->Length >= LENGTH_OF_CONFIRM &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_CONFIRM) == COMMAND_CONFIRM) {
					helpUserWithConfirm();
				} else if (inputBox->Text->Length >= LENGTH_OF_CLEAR &&
				 	inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_CLEAR) == COMMAND_CLEAR){
					helpUserWithClear();
				} else if (inputBox->Text->Length >= LENGTH_OF_UNDO &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_UNDO) == COMMAND_UNDO){
					helpUserWithUndo();
				}

}

bool UI::TaskItGUI::checkPrevCommandEqualCurrentCommand() {
				int lengthOfCommand = inputBox->Text->IndexOf(SPACE);
				if (lengthOfCommand >= BEGINNING_POS_OF_STR) {
					setCurrCommand(inputBox->Text->Substring(BEGINNING_POS_OF_STR, lengthOfCommand));
				} else {
					return false;
				}
				if (_currCommand == _prevCommand) {
					return true;
				} else {
					return false;
				}
}

bool UI::TaskItGUI::checkIfNumber(String^ input) {
				if (input == DIGIT_ONE) {
					return true;
				} else if (input == DIGIT_TWO) {
					return true;
				} else if (input == DIGIT_THREE) {
					return true;
				} else if (input == DIGIT_FOUR) {
					return true;
				} else if (input == DIGIT_FIVE) {
					return true;
				} else if (input == DIGIT_SIX) {
					return true;
				} else if (input == DIGIT_SEVEN) {
					return true;
				} else if (input == DIGIT_EIGHT) {
					return true;
				} else if (input == DIGIT_NINE) {
					return true;
				} else if (input == DIGIT_ZERO) {
					return true;
				}
				return false;
}

void UI::TaskItGUI::startTrackingFieldNo() {
				_currFieldNo = FIRST_FIELD;
}

void UI::TaskItGUI::helpUserWithAdd() {
				inputBox->Text = ADD_WITH_PROMPTS;
				inputBox->SelectionStart = POS_AFTER_FIRST_OPEN_BRACKET_FOR_ADD;
				inputBox->SelectionLength = LENGTH_OF_ACTIVITY;
				setPrevCommand(COMMAND_ADD);
				startTrackingFieldNo();
}

void UI::TaskItGUI::helpUserWithEdit() {
				inputBox->Text = EDIT_WITH_BASIC_PROMPTS;
				inputBox->SelectionStart = POS_AFTER_FIRST_OPEN_BRACKET_FOR_EDIT;
				inputBox->SelectionLength = LENGTH_OF_NO_DOT;
				setPrevCommand(COMMAND_EDIT);
				startTrackingFieldNo();
}

void UI::TaskItGUI::helpUserWithFDelete() {
				inputBox->Text = FDELETE_WITH_PROMPTS;
				inputBox->SelectionStart = POS_AFTER_FIRST_OPEN_BRACKET_FOR_FDELETE;
				inputBox->SelectionLength = LENGTH_OF_NO_DOT;
				setPrevCommand(COMMAND_FDELETE);
				startTrackingFieldNo();
}

void UI::TaskItGUI::helpUserWithFEdit() {
				inputBox->Text = FEDIT_WITH_BASIC_PROMPTS;
				inputBox->SelectionStart = POS_AFTER_FIRST_OPEN_BRACKET_FOR_FEDIT;
				inputBox->SelectionLength = LENGTH_OF_NO_DOT;
				setPrevCommand(COMMAND_FEDIT);
				startTrackingFieldNo();
}

void UI::TaskItGUI::helpUserWithView() {
				inputBox->Text = VIEW_WITH_PROMPTS;
				inputBox->SelectionStart = POS_AFTER_FIRST_OPEN_BRACKET_FOR_VIEW;
				inputBox->SelectionLength = LENGTH_OF_ALL_CATEGORY_DATE;
				setPrevCommand(COMMAND_VIEW);
				startTrackingFieldNo();
}

void UI::TaskItGUI::helpUserWithDelete() {
				inputBox->Text = DELETE_WITH_PROMPTS;
				inputBox->SelectionStart = POS_AFTER_FIRST_OPEN_BRACKET_FOR_DELETE;
				inputBox->SelectionLength = LENGTH_OF_NO_DOT;
				setPrevCommand(COMMAND_DELETE);
				startTrackingFieldNo();
}

void UI::TaskItGUI::helpUserWithSearch() {
				inputBox->Text = SEARCH_WITH_PROMPTS;
				inputBox->SelectionStart = POS_AFTER_FIRST_OPEN_BRACKET_FOR_SEARCH;
				inputBox->SelectionLength = LENGTH_OF_KEYWORD;
				setPrevCommand(COMMAND_SEARCH);
				startTrackingFieldNo();

}

void UI::TaskItGUI::helpUserWithMark() {
				inputBox->Text = MARK_WITH_PROMPTS;
				inputBox->SelectionStart = POS_AFTER_FIRST_OPEN_BRACKET_FOR_MARK;
				inputBox->SelectionLength = LENGTH_OF_NO_DOT;
				setPrevCommand(COMMAND_MARK);
				startTrackingFieldNo();
}

void UI::TaskItGUI::helpUserWithBlock() {
				inputBox->Text = BLOCK_WITH_PROMPTS;
				inputBox->SelectionStart = POS_AFTER_FIRST_OPEN_BRACKET_FOR_BLOCK;
				inputBox->SelectionLength = LENGTH_OF_ACTIVITY;
				setPrevCommand(COMMAND_BLOCK);
				startTrackingFieldNo();
}

void UI::TaskItGUI::helpUserWithUnmark() {
				inputBox->Text = UNMARK_WITH_PROMPTS;
				inputBox->SelectionStart = POS_AFTER_FIRST_OPEN_BRACKET_FOR_UNMARK;
				inputBox->SelectionLength = LENGTH_OF_NO_DOT;
				setPrevCommand(COMMAND_UNMARK);
				startTrackingFieldNo();
}

void UI::TaskItGUI::helpUserWithConfirm() {
				inputBox->Text = CONFIRM_WITH_PROMPTS;
				inputBox->SelectionStart = POS_AFTER_FIRST_OPEN_BRACKET_FOR_COMFIRM;
				inputBox->SelectionLength = LENGTH_OF_NO_DOT;
				setPrevCommand(COMMAND_CONFIRM);
				startTrackingFieldNo();
}

void UI::TaskItGUI::helpUserWithClear() {
			inputBox->Text = COMMAND_CLEAR;
			inputBox->SelectionStart = BEGINNING_POS_OF_STR;
			inputBox->SelectionLength = LENGTH_OF_CLEAR;
			setPrevCommand(COMMAND_CLEAR);
			startTrackingFieldNo();
}

void UI::TaskItGUI::helpUserWithUndo() {
			inputBox->Text = COMMAND_UNDO;
			inputBox->SelectionStart = BEGINNING_POS_OF_STR;
			inputBox->SelectionLength = LENGTH_OF_UNDO;
			setPrevCommand(COMMAND_UNDO);
			startTrackingFieldNo();
}

void UI::TaskItGUI::getAdvancedEditPrompts(int noOfDigits) {
				string editItemNo;
				String^ itemDetails;
				bool isValidItemNo = false;
				if (_currentEditItemNo != _previousEditItemNo) {
					editItemNo = convertToString(inputBox->Text->Substring(POS_AFTER_FIRST_OPEN_BRACKET_FOR_EDIT, noOfDigits));
					itemDetails = convertToCliString(userScheduler->checkEditIndex(editItemNo, isValidItemNo));
					if (isValidItemNo) {
						inputBox->Text = COMMAND_EDIT + SPACE + OPEN_SQ_BRACKET + _currentEditItemNo + CLOSE_SQ_BRACKET + itemDetails;
						_previousEditItemNo = _currentEditItemNo;
						inputBox->SelectionStart = POS_AFTER_FIRST_OPEN_BRACKET_FOR_EDIT + noOfDigits;
						startTrackingFieldNo();
					} else {
						inputBox->Text = COMMAND_EDIT + SPACE + OPEN_SQ_BRACKET + _currentEditItemNo + CLOSE_SQ_BRACKET;
						_previousEditItemNo = _currentEditItemNo;
						inputBox->SelectionStart = POS_AFTER_FIRST_OPEN_BRACKET_FOR_EDIT + noOfDigits;
						startTrackingFieldNo();
					}
				}
}

void UI::TaskItGUI::getAdvancedFEditPrompts(int noOfDigits) {
				string editItemNo;
				String^ itemDetails;
				bool isValidItemNo = false;
				if (_currentEditItemNo != _previousEditItemNo) {
					editItemNo = convertToString(inputBox->Text->Substring(POS_AFTER_FIRST_OPEN_BRACKET_FOR_FEDIT, noOfDigits));
					itemDetails = convertToCliString(userScheduler->checkFEditIndex(editItemNo, isValidItemNo));
					if (isValidItemNo) {
						inputBox->Text = COMMAND_FEDIT + SPACE + OPEN_SQ_BRACKET + _currentEditItemNo + CLOSE_SQ_BRACKET + itemDetails;
						_previousEditItemNo = _currentEditItemNo;
						inputBox->SelectionStart = POS_AFTER_FIRST_OPEN_BRACKET_FOR_FEDIT + noOfDigits;
						startTrackingFieldNo();
					} else {
						inputBox->Text = COMMAND_FEDIT + SPACE + OPEN_SQ_BRACKET + _currentEditItemNo + CLOSE_SQ_BRACKET;
						_previousEditItemNo = _currentEditItemNo;
						inputBox->SelectionStart = POS_AFTER_FIRST_OPEN_BRACKET_FOR_FEDIT + noOfDigits;
						startTrackingFieldNo();
					}
				}
}

bool UI::TaskItGUI::checkIfEditInputsAreNumbers(int noOfChar) {
				String^ currentChar;
				bool isNumber = true;
				_currentEditItemNo = inputBox->Text->Substring(POS_AFTER_FIRST_OPEN_BRACKET_FOR_EDIT, noOfChar);
				for (int i = INITIAL_VALUE; i < noOfChar; i++) {
					currentChar = inputBox->Text->Substring(POS_AFTER_FIRST_OPEN_BRACKET_FOR_EDIT + i, LENGTH_OF_A_DIGIT);
					isNumber = checkIfNumber(currentChar);
					if (!isNumber) {
						return false;
					}
				}
				return true;
}

bool UI::TaskItGUI::checkIfFEditInputsAreNumbers(int noOfChar) {
				String^ currentChar;
				bool isNumber = true;
				_currentEditItemNo = inputBox->Text->Substring(POS_AFTER_FIRST_OPEN_BRACKET_FOR_FEDIT, noOfChar);
				for (int i = INITIAL_VALUE; i < noOfChar; i++) {
					currentChar = inputBox->Text->Substring(POS_AFTER_FIRST_OPEN_BRACKET_FOR_FEDIT + i, LENGTH_OF_A_DIGIT);
					isNumber = checkIfNumber(currentChar);
					if (!isNumber) {
						return false;
					}
				}
				return true;

}

//@author A0080442N
bool UI::TaskItGUI::checkIfBasicEditPromptsPresent() {
				if (inputBox->Text->Length >= (LENGTH_OF_EDIT_BASIC_PROMPT_NO_DIGIT) &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_EDIT) == COMMAND_EDIT) {
					if (inputBox->Text->IndexOf(OPEN_SQ_BRACKET) == POS_FIRST_OPEN_BRACKET_FOR_EDIT) {
						if (inputBox->Text->IndexOf(CLOSE_SQ_BRACKET) == POS_FIRST_CLOSE_BRACKET_EDIT_ONE_DIGIT) { //single digit
							return true;
						} else if (inputBox->Text->IndexOf(CLOSE_SQ_BRACKET) == POS_FIRST_CLOSE_BRACKET_EDIT_TWO_DIGITS) { // double digit
							return true;
						} else if (inputBox->Text->IndexOf(CLOSE_SQ_BRACKET) == POS_FIRST_CLOSE_BRACKET_EDIT_THREE_DIGITS) { // triple digit
							return true;
						} else if (inputBox->Text->IndexOf(CLOSE_SQ_BRACKET) == POS_FIRST_CLOSE_BRACKET_EDIT_FOUR_DIGITS) { // four digit
							return true;
						}
					}
				}
				return false;
}

bool UI::TaskItGUI::checkIfBasicFEditPromptsPresent() {
				if (inputBox->Text->Length >= (LENGTH_OF_FEDIT_BASIC_PROMPT_NO_DIGIT) &&
					inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_FEDIT) == COMMAND_FEDIT) {
					if (inputBox->Text->IndexOf(OPEN_SQ_BRACKET) == POS_FIRST_OPEN_BRACKET_FOR_FEDIT) {
						if (inputBox->Text->IndexOf(CLOSE_SQ_BRACKET) == POS_FIRST_CLOSE_BRACKET_FEDIT_ONE_DIGIT) { //single digit
							return true;
						}else if (inputBox->Text->IndexOf(CLOSE_SQ_BRACKET) == POS_FIRST_CLOSE_BRACKET_FEDIT_TWO_DIGITS) { // double digit
							return true;
						}else if (inputBox->Text->IndexOf(CLOSE_SQ_BRACKET) == POS_FIRST_CLOSE_BRACKET_FEDIT_THREE_DIGITS) { // triple digit
							return true;
						}else if (inputBox->Text->IndexOf(CLOSE_SQ_BRACKET) == POS_FIRST_CLOSE_BRACKET_FEDIT_FOUR_DIGITS) { // four digit
							return true;
						}
					}
				}
				return false;
}

bool UI::TaskItGUI::checkIfFirstWordCommand() {
				if (inputBox->Text->Length >= LENGTH_OF_A_SEVEN_LETTER_COMMAND) {
					if (inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_FDELETE) == COMMAND_FDELETE ||
						inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_CONFIRM) == COMMAND_CONFIRM) {
						return true;
					}
				}
				if (inputBox->Text->Length >= LENGTH_OF_A_SIX_LETTER_COMMAND) {
					if (inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_SEARCH) == COMMAND_SEARCH ||
						inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_DELETE) == COMMAND_DELETE ||
						inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_UNMARK) == COMMAND_UNMARK) {
						return true;
					}
				}
				if (inputBox->Text->Length >= LENGTH_OF_A_FIVE_LETTER_COMMAND) {
					if (inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_FEDIT) == COMMAND_FEDIT ||
						inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_CLEAR) == COMMAND_CLEAR ||
						inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_BLOCK) == COMMAND_BLOCK) {
						return true;
					}
				}
				if (inputBox->Text->Length >= LENGTH_OF_A_FOUR_LETTER_COMMAND) {
					if (inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_HELP) == COMMAND_HELP ||
						inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_UNDO) == COMMAND_UNDO ||
						inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_VIEW) == COMMAND_VIEW ||
						inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_MARK) == COMMAND_MARK ||
						inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_EDIT) == COMMAND_EDIT) {
						return true;
					}
				}
				if (inputBox->Text->Length >= LENGTH_OF_A_THREE_LETTER_COMMAND) {
					if (inputBox->Text->Substring(BEGINNING_POS_OF_STR, LENGTH_OF_ADD) == COMMAND_ADD) {
						return true;
					}
				}
				return false;
}

void UI::TaskItGUI::setPrevCommand(String^ prevCommand) {
				_prevCommand = prevCommand;
}

void UI::TaskItGUI::setCurrCommand(String^ currCommand) {
				_currCommand = currCommand;
}

void UI::TaskItGUI::setUsername(String^ username) {
				_username = username;
}

String^ UI::TaskItGUI::getUsername() {
				return _username;
}

//@author A0096509U
void UI::TaskItGUI::addUserInputLine(String^ currentUserInput) {
				_allUserInputs.push_back(currentUserInput);
}

String^ UI::TaskItGUI::getUserInputLine(int index) {
				return _allUserInputs.at(index);
}

int UI::TaskItGUI::getTotalLinesEntered() {
				return _allUserInputs.size();
}

int UI::TaskItGUI::getCurrentLinePos() {
				return _currentLinePos;
}

void UI::TaskItGUI::setCurrentLinePos(int pos) {
				_currentLinePos = pos;
}

void UI::TaskItGUI::setTempUserLine(String^ userInput) {
				_tempUserLine = userInput;
}

String^ UI::TaskItGUI::getTempUserLine() {
				return _tempUserLine;
}

string UI::TaskItGUI::convertToString(String^ input) {
				std::string converted_input = msclr::interop::marshal_as< std::string >(input);
				return converted_input;
}

String^ UI::TaskItGUI::convertToCliString(string input) {
				String^ converted_input = msclr::interop::marshal_as< String^ >(input);
				return converted_input;
}

bool UI::TaskItGUI::noTextAfterCommand(){
				 int posOfSpace = inputBox->Text->IndexOf(SPACE);
				 if (posOfSpace == INVALID_NO_SPACE) {
					 return true;
				 }
				 return false;
}